package com.zyy.smartcity1129.bus;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.MainActivity;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.BusLineInfo;
import com.zyy.smartcity1129.data.BusOrderInfo;
import com.zyy.smartcity1129.data.BusStopInfo;
import com.zyy.smartcity1129.util.Http;

import java.util.List;

import okhttp3.RequestBody;
import okhttp3.internal.http2.Http2Stream;

/**
 * @ClassName: BusConfirm$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 5/12/2021$
 * @Version: 1.0
 */
public class BusConfirmFragment extends BaseFragment {
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;
    private TextView tv5;
    private Button btnConfirm;
    private BusLineInfo.RowsBean busLine = BusFragment.line;
    private List<BusStopInfo.RowsBean> stop = BusFragment.stop;
    private String name = BusUpDownFragment.name;
    private String phone = BusUpDownFragment.phone;
    private String ups = BusUpDownFragment.ups;
    private String downs = BusUpDownFragment.downs;
    private String date = BusDateFragment.date;
    private String path = BusFragment.path;
    private double price = BusFragment.price;
    public static String orderNum;

    @Override
    protected int layId() {
        return R.layout.f_busconfirm;
    }

    @Override
    protected String title() {
        return "订单确认";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        BusFragment.liveData.observe(getViewLifecycleOwner(), m -> {
            remove();
        });
        tv1.setText("乘客姓名：" + name);
        tv2.setText("手机号码：" + phone);
        tv3.setText("上车地点：" + ups);
        tv4.setText("下车地点：" + downs);
        tv5.setText("乘车日期：" + date);
        btnConfirm.setOnClickListener(view -> {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("start", ups);
            jsonObject.addProperty("end", downs);
            jsonObject.addProperty("path", path);
            jsonObject.addProperty("price", price);
            jsonObject.addProperty("status", "0");
            th(() -> {
                BusOrderInfo info = Http.getInstance().get("/prod-api/api/bus/order", RequestBody.create(Http.m, jsonObject.toString()), BusOrderInfo.class, 2);
                ui(() -> {
                    if (info.getMsg().equals("操作成功")) {
                        orderNum = info.getOrderNum();
                        startActivity(new Intent(getContext(), MainActivity.class));
                        getActivity().finish();
                    } else {
                        Toast.makeText(getContext(), info.getMsg(), Toast.LENGTH_SHORT).show();
                    }
                });
            });
        });
    }

    private void initView(View iv) {
        tv1 = (TextView) iv.findViewById(R.id.tv1);
        tv2 = (TextView) iv.findViewById(R.id.tv2);
        tv3 = (TextView) iv.findViewById(R.id.tv3);
        tv4 = (TextView) iv.findViewById(R.id.tv4);
        tv5 = (TextView) iv.findViewById(R.id.tv5);
        btnConfirm = (Button) iv.findViewById(R.id.btnConfirm);
    }
}
